/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import distributedObject from '@ohos.data.distributedDataObject'
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
import worker from '@ohos.worker';

var _25data = "123456789012345678901235";

function grantPermission() {
    console.info('hanlu grantPermission')
    let context = featureAbility.getContext()
    let bundleFlag = 0
    let tokenId = undefined
    let userId = 100
    let appInfo = bundle.getApplicationInfo('ohos.samples.jshelloworld', bundleFlag, userId)
    tokenId = appInfo.accessTokenId
    console.info(`hanlu grantPermission,tokenId=${tokenId}`)

    context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) {
        console.info(`result.requestCode=${result.requestCode}`)
    })
}

function generateObject() {
    //    let result = {};
    //    for (let i = 0;i < 80; i++) {
    //        result["name" + i] = _25data + i;
    //    }
    //    return result;
    return { name: "amy", isVis: false };
}

var g_object = distributedObject.createDistributedObject(generateObject());
var changFlag = false;

export default {
    changeCallback: function (sessionId, changeData) {
        // console.info("hanlu inin change111" + sessionId + " " + this.response);
        console.info("hanlu inin change111");
        if (changeData != null && changeData != undefined) {
            changeData.forEach(element => {
                console.info("hanlu changed !" + element + " " + g_object[element]);
                this.response += "\nchanged " + element;
            });
        }
        console.info("hanlu inin change111 end" + sessionId + " " + changeData);
    },
    statusCallback: function (sessionId, networkid, status) {
        console.info("hanlu inin change111" + sessionId + " " + this.response);
        this.response += "\nstatus changed " + sessionId + " " + status + " " + networkid;
    },
    data: {
        response: "",
        name: "jack",
        worker: new worker.Worker("workers/worker.js", {
            name: "worker_test"
        })
    },
    onInit() {
        grantPermission();
        this.title = this.$t('strings.world');
    },
    joinSessionOnclick: function () {
        console.info("hanlu start call onclick changeSessionOnclick33");
        this.response = "start joinSession";
        g_object.setSessionId("123456");
        if (g_object != undefined && g_object != null) {
            this.response += "\nchangeSession success " + g_object.__sessionId;
        } else {
            this.response += "\nchangeSession fail";
        }
        let obj = {
            type: "normal"
        }
        console.info('[worker.index] sendString:')
        this.worker.postMessage(obj)
    },
    leaveSessionOnclick: function () {
        console.info("hanlu start call onclick leaveSessionOnclick");
        g_object.setSessionId("");
        this.response += "\nleaveSession success";
    },
    setNameOnclick: function () {
        //        console.info("hanlu start call onclick setNameOnclick");
        //        for (let i = 0;i < 80; i++) {
        //            g_object["name" + i] = _25data;
        //        }
        //        console.info("hanlu endset");
        //        console.info("hanlu start get");
        //        for (let i = 0;i < 80; i++) {
        //            console.info("name" + i + " = " + g_object["name" + i]);
        //        }
        //        console.info("hanlu endget");

        this.response += "\nstart setName";
        if (g_object != undefined && g_object != null) {
            if (g_object.isVis) {
                console.info("hanlu start set");
                g_object.name = "jack"
                // g_object.name = "10123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
                //                g_object.age = 19;
                g_object.isVis = false;
                //                g_object.parent = {mother:"jack mom",father:"jack Dad"};
                //                g_object.list = [{mother:"jack mom"}, {father:"jack Dad"}];
            } else {
                console.info("hanlu start set");
                g_object.name = "jack1"
                //                g_object.parent.mother = "mother jack1 change";
                // g_object.name = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
                //                g_object.age = 8;
                g_object.isVis = true;
                //                g_object.parent = {mother:"jack1 mom",father:"jack1 Dad"};
                //                g_object.list = [{mother:"jack1 mom"}, {father:"jack1 Dad"}];
            }
            this.response += "\nsetName success " + g_object.parent.mother;
        } else {
            this.response += "\nobject is null,set name fail";
        }
    },
    getNameOnclick: function () {
        console.info("hanlu start call onclick getNameOnclick");
        this.response += "\nstart setName";
        if (g_object != undefined && g_object != null) {
            this.response += "\nname " + g_object.name;
            this.response += "\nmom " + g_object.parent.mother;
            this.response += "\ndad " + g_object.list[1].father;
        } else {
            this.response += "\nobject is null,get name fail";
        }
    },
    onOnclick: function () {
        console.info("hanlu start call watch change");
        this.response += "\nstart watch";
        g_object.on("change", this.changeCallback);
        this.response += "\nwatch success ";
    },
    offOnclick: function () {
        console.info("hanlu start call unwatch change");
        this.response += "\nstart unwatch";
        g_object.off("change");
        this.response += "\nunwatch success ";
    },
    onStatusclick: function () {
        console.info("hanlu start call watch status");
        this.response += "\nstart watch status";
        g_object.on("status", this.statusCallback);
        this.response += "\nwatch success ";
    },
    offStatusclick: function () {
        console.info("hanlu start call unwatch status");
        this.response += "\nstart unwatch";
        g_object.off("status", this.statusCallback);
        this.response += "\nunwatch success ";
    }
}


